package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.common.UserSessionUtils;
import com.example.demo.entity.Articleinfo;
import com.example.demo.entity.Userinfo;
import com.example.demo.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("/mylist")
    public AjaxResult getMyList(HttpServletRequest request){
        Userinfo userinfo= UserSessionUtils.getSessionUser(request);
        if(userinfo==null){
            return AjaxResult.fail(-1,"非法请求");
        }
        List<Articleinfo> list=articleService.getMyList(userinfo.getId());
        return  AjaxResult.success(list);
    }

    @RequestMapping("/del")
    public AjaxResult del(Integer id,HttpServletRequest request){
        if(id==null && id<=0){
             //参数有误
            return AjaxResult.fail(-1,"参数异常");
        }
        Userinfo user=UserSessionUtils.getSessionUser(request);
        if(user==null){
            return AjaxResult.fail(-2,"用户未登陆");
        }
         return AjaxResult.success(articleService.del(id,user.getId()));
    }

    @RequestMapping("/detail")
    public AjaxResult getDetail(Integer id){
        //非空校验
        if(id==null || id<0){
            return AjaxResult.fail(-1,"非法参数");
        }
        return AjaxResult.success(articleService.getDetail(id));
    }

    @RequestMapping("/incr-rcount")
    public AjaxResult incrRCount(Integer id){
        if(id!=null && id>0){
            return  AjaxResult.success(articleService.incrRCount(id));
        }
        return AjaxResult.fail(-1,"未知错误");
    }

    @RequestMapping("/add")
    public AjaxResult add(Articleinfo articleinfo,HttpServletRequest request){
        //1.非空校验
        if(articleinfo==null || !StringUtils.hasLength(articleinfo.getTitle()) ||
         !StringUtils.hasLength(articleinfo.getContent())){
            //非法参数
            return AjaxResult.fail(-1,"非法参数");
        }
        //2.数据库添加操作
        //a.得到当前用户的uid
        Userinfo userinfo=UserSessionUtils.getSessionUser(request);
        if(userinfo==null || userinfo.getId()<=0){
            //无效的用户
            return AjaxResult.fail(-2,"无效的登陆用户");
        }
        articleinfo.setUid(userinfo.getId());
        return AjaxResult.success(articleService.add(articleinfo));
    }

    @RequestMapping("/update")
    public AjaxResult update(Articleinfo articleinfo,HttpServletRequest request){
        if(articleinfo==null || !StringUtils.hasLength(articleinfo.getTitle())
                || !StringUtils.hasLength(articleinfo.getContent()) ||
                articleinfo.getId()==null){
            //非法参数
            return AjaxResult.fail(-1,"非法参数");
        }
        //得到当前登陆用户的ID
        Userinfo userinfo=UserSessionUtils.getSessionUser(request);
        if(userinfo==null && userinfo.getId()==null){
            //无效登陆用户
            return AjaxResult.fail(-2,"无效用户");
        }
        //核心代码（解决了修改文章归属人)
        articleinfo.setUid(userinfo.getId());
        articleinfo.setUpdatetime(LocalDateTime.now());
        return AjaxResult.success(articleService.update(articleinfo));
    }

    @RequestMapping("/listbypage")
    //pindex为当前页面，且至少为1
    //psize为每页的显示条数
    public AjaxResult getListByPage(Integer pindex,Integer psize){
        //1.参数矫正
        if(pindex==null || pindex<=1){
            pindex=1;
        }
        if(psize==null || psize<=1){
            psize=2;
        }
        //分页公式的值(当前页码-1)*每页显示数
        int offset=(pindex-1)*psize;
        List<Articleinfo> list=articleService.getListByPage(psize,offset);
        //当前列表有多少页
        //a.总共有多少条数据
         int totalCount=articleService.getCount();
        //b.总条数/psize(每页显示条数)
        double pcountdb=totalCount/(psize*1.0);
        //c.使用进一法得到总页数
        int pcount=(int)Math.ceil(pcountdb);
        HashMap<String,Object> result=new HashMap<>();
        result.put("list",list);
        result.put("pcount",pcount);
        return AjaxResult.success(result);
    }
}
